/// [Consecutive prime sum](https://projecteuler.net/problem=51)
///
/// 假设每次变化m位，n为正整数；
/// 当m=3n-2时，m*2=6n-4∈3n-1，m*3=9n-6∈3n，m的倍数有3n、3n-1、3n-2，因此最少有3个产生的值可以被3整除；
/// 当m=3n-1时，m*2=6n-2∈3n-2，m*3=9n-3∈3n，同上；
/// 当m=3n时，m*2=6n∈3n，m*3=9n∈3n，m的倍数只有3n。
///
/// 需要获取8个质数，因此需要同时变化3的倍数位，且最后一位不能变化（非奇即偶）
///
/// 注意：首位不为0
fn main() {
    // 4-digit
    for a in (0..5).map(|x| x * 2 + 1).filter(|x| x % 3 != 0) {
        let count = (1..=9)
            .map(|x| a + x * 1_110)
            .filter(|&x| is_prime(x))
            .collect::<Vec<_>>();
        if count.len() > 2 {
            println!("xxx{}: {:?}", a, count);
        }
    }
    // 5-digit
    for a in (0..5).map(|x| x * 2 + 1) {
        for b in (1..=9).filter(|b| (a + b) % 3 != 0) {
            let count = (0..=9)
                .map(|x| a + x * 1_110 + b * 10_000)
                .filter(|&x| is_prime(x))
                .collect::<Vec<_>>();
            if count.len() > 7 {
                println!("{}xxx{}: {:?}", b, a, count);
            }
        }
        for b in (0..=9).filter(|b| (a + b) % 3 != 0) {
            let list = [10, 100, 1_000, 10_000];
            for i in 0..3 {
                let count = (1..=9)
                    .map(|x| a + b * list[i] + x * 11_110 - x * list[i])
                    .filter(|&x| is_prime(x))
                    .collect::<Vec<_>>();
                if count.len() > 7 {
                    println!("{}{}{}{}: {:?}", "x".repeat(3 - i), b, "x".repeat(i), a, count);
                }
            }
        }
    }
    // 6-digit
    for a in (0..5).map(|x| x * 2 + 1) {
        for b in 1..=9 {
            for c in (0..=9).filter(|c| (a + b + c) % 3 != 0) {
                let list = [10, 100, 1_000, 10_000];
                for i in 0..4 {
                    let count = (0..=9)
                        .map(|x| a + b * 100_000 + c * list[i] + x * 11_110 - x * list[i])
                        .filter(|&x| is_prime(x))
                        .collect::<Vec<_>>();
                    if count.len() > 7 {
                        println!("{}{}{}{}{}: {:?}", b, "x".repeat(3 - i), c, "x".repeat(i), a, count);
                    }
                }
            }
        }
        for b in 0..=9 {
            for c in (0..=9).filter(|c| (a + b + c) % 3 != 0) {
                let list = [10, 100, 1_000, 10_000, 100_000];
                for i in 0..4 {
                    for j in 0..i {
                        let count = (1..=9)
                            .map(|x| a + b * list[i] + c * list[j] + x * 111_110 - x * list[i] - x * list[j])
                            .filter(|&x| is_prime(x))
                            .collect::<Vec<_>>();
                        if count.len() > 7 {
                            println!(
                                "{}{}{}{}{}{}: {:?}",
                                "x".repeat(4 - i),
                                b,
                                "x".repeat(i - j - 1),
                                c,
                                "x".repeat(j),
                                a,
                                count
                            );
                        }
                    }
                }
            }
        }
    }
}

fn is_prime(n: usize) -> bool {
    if n < 2 {
        return false;
    }
    for i in 2..n {
        if n % i == 0 {
            return false;
        }
        if i.pow(2) > n {
            break;
        }
    }
    true
}
